package com.seckill.seckilldemo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.seckill.seckilldemo.pojo.User;
import com.seckill.seckilldemo.vo.RespBean;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/*
生成用户
* */
public class UserUtil {
    private static void createUser(int count) throws Exception {
        List<User> users = new ArrayList<>(count);
        for(int i = 0 ; i < count ; i ++){
            User user = new User();
            user.setId(13000000000L+i);
            user.setNickname("user"+i);
            user.setSlat("1a2b3c4d");
            user.setPassword(MD5Util.inputPassToDBPass("123456",user.getSlat()));
            user.setLoginCount(1);
            user.setRegisterDate(new Date());
            users.add(user);
        }
        System.out.println("已创建"+count+"个用户");
        //插入数据库
        Connection conn = getConn();
        String sql = "insert into t_user(id,nickname,password,slat,register_date,login_count)" +
                "values(?,?,?,?,?,?)";
        PreparedStatement pst = conn.prepareStatement(sql);
        for(int i = 0 ; i < users.size() ; i ++){
            User user = users.get(i);
            pst.setLong(1,user.getId());
            pst.setString(2,user.getNickname());
            pst.setString(3,user.getPassword());
            pst.setString(4,user.getSlat());
            pst.setTimestamp(5,new Timestamp(user.getRegisterDate().getTime()));
            pst.setInt(6,user.getLoginCount());
            pst.addBatch();
        }
        pst.executeBatch();
        pst.clearParameters();
        conn.close();
        //登录，生成userTicket
        String urlString = "http://localhost:8080/login/doLogin";
        File file = new File("C:\\Users\\asus\\Desktop\\config.txt");
        if(file.exists()){
            file.delete();
        }
        //读写
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        file.createNewFile();
        randomAccessFile.seek(0);
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            URL url = new URL(urlString);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream out = httpURLConnection.getOutputStream();
            String params = "mobile=" + user.getId() + "&password=" + MD5Util.inputPassToFormPass("123456");
            out.write(params.getBytes());
            out.flush();
            InputStream in = httpURLConnection.getInputStream();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int len = 0;
            while((len = in.read(buff))>=0){
                bout.write(buff,0,len);
            }
            in.close();
            bout.close();
            String response = new String(bout.toByteArray());
            ObjectMapper mapper = new ObjectMapper();
            RespBean respBean = mapper.readValue(response, RespBean.class);
            String userTicket = (String) respBean.getObj();
            System.out.println("创建userTicket:"+userTicket);
            System.out.println(respBean);
            String row = user.getId() + "," + userTicket;
            randomAccessFile.seek(randomAccessFile.length());
            randomAccessFile.write(row.getBytes());
            randomAccessFile.write("\r\n".getBytes());
            System.out.println("写入文件:");
        }
        randomAccessFile.close();
        System.out.println("结束");
    }
    /*driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.0.178:3306/seckill?useUnicode=true&charactertEncoding=UTF-8
    username: root
    password: root*/
    private static Connection getConn() throws Exception{
        String url = "jdbc:mysql://localhost:3306/seckill?useUnicode=true&charactertEncoding=UTF-8";
        String username = "root";
        String password = "root";
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        return DriverManager.getConnection(url,username,password);
    }

    public static void main(String[] args) throws Exception {
        createUser(5000);
    }
}
